import axios from '@/axios.config'

export default {
    state: {
        msg: '',
        flag: 0,
        taskList: [],
        userList:[],
        userTable:[],
        dispatchTaskList:[],
        totleNum: 0,
        totleNum2: 0,
        verifyCode: 0,
        num:0,
        logList:[],
        logNum:0,
        extra: {},
        showStatus: 0,
        extraFlag: null,
        kkList: [],
        emailList: [],
        kkNum: 0,
        emailNum: 0
    },
    mutations: {
        send_msg(state, payload) {
            state.msg = payload._msg,
            state.flag = payload._flag
        },
        add_tasklist(state, payload) {
            state.taskList = [].concat(payload.com_tasklist);
            state.totleNum = payload.com_totle;
        },
        add_user_list(state, payload) {
            state.userList = [].concat(payload.com_user_list);
            state.extraFlag = payload.com_extra_flag
        },
        add_user_table(state, payload) {
            state.userTable = [].concat(payload.com_user_table);
            state.num = payload.com_num;
            state.extra = Object.assign({},payload.com_extra);
            state.showStatus = payload.com_status;
        },
        add_dispatchlist(state, payload) {
            state.dispatchTaskList = [].concat(payload.com_dispatchlist)
            state.totleNum2 = payload.com_totle;
        },
        add_verify(state, payload) {
            state.verifyCode = payload.com_code
        },
        add_log(state, payload) {
            state.logList = [].concat(payload.com_loglist);
            state.logNum = payload.com_totle;
        },
        add_kk_list(state, payload) {
            state.kkList = [].concat(payload.com_kklist);
            state.kkNum = payload.com_totle;
        },
        add_email_list(state, payload) {
            state.emailList = [].concat(payload.com_emaillist);
            state.emailNum = payload.com_totle;
        },
    },
    actions: {
        //任务调度频率相关接口
        //新建任务频率
        createTaskFre({ commit }, payload) {
            return axios.post('/api/schedule/create', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "添加成功",
                            _flag: 0
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                            _flag: 1
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                        _flag: 1
                    })
                })
        },
        //编辑任务频率
        editTaskFre({ commit }, payload) {
            return axios.post('/api/schedule/update', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "编辑成功",
                            _flag: 0
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                            _flag: 1
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                        _flag: 1
                    })
                })
        },
        //删除任务频率
        delTaskFre({ commit }, payload) {
            return axios.post('/api/schedule/del', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "删除成功"
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //获取任务列表
        getTaskList({ commit }, payload) {
            return axios.post('/api/schedule/list', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_tasklist', {
                            com_tasklist: res.data.data.t,
                            com_totle: res.data.data.totalCount
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //提交归档
        logStart({ commit }, payload) {
            return axios.post('/api/log/start', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "归档成功"
                        })
                    }else if(res.data.retCode == 9999995){
                        commit('add_verify',{
                            com_code:res.data.retCode
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //user的表盘sql列表
        getUserSql({ commit }, payload) {
            return axios.post('/api/auth/data/list', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_user_list', {
                            com_user_list: res.data.data.t,
                            com_extra_flag: res.data.extra
                        })
                    }else{/*  */
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //table排序
        sortTables({ commit }, payload) {
            return axios.post('/api/dashboard/sort', payload)
                .then((res) => {
                    
                    // if(res.data.retCode == 0) {
                    //     commit('send_msg', {
                    //         _msg: res.data.retMsg
                    //     })
                    // }else{
                    //     commit('send_msg', {
                    //         _msg: res.data.retMsg
                    //     })
                    // }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //获取table
        getUserTable({ commit }, payload) {
            return axios.post('/api/auth/data/info', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        if(res.data.data == null) {
                            commit('add_user_table', {
                                com_user_table: [],
                                com_num: 0,
                                com_extra: res.data.extra,
                                com_status: res.data.extra.status,
                            })
                        }else{
                            commit('add_user_table', {
                                com_user_table: res.data.data,
                                com_num: res.data.data,
                                com_extra: res.data.extra,
                                com_status: res.data.extra.status,
                            })
                        }
                    }else{
                        commit('add_user_table', {
                            com_user_table: []
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //新增任务调度
        addNewDispatch({ commit }, payload) {
            return axios.post('/api/task/create', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "新增任务调度成功",
                            _flag: 0
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                            _flag: 1
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                        _flag: 1
                    })
                })
        },
        //任务调度列表
        taskDispatchList({ commit }, payload) {
            return axios.post('/api/task/list', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_dispatchlist', {
                            com_dispatchlist: res.data.data.t,
                            com_totle: res.data.data.totalCount
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //更新调度任务状态
        updateDispatchStatus({ commit }, payload) {
            return axios.post('/api/task/status', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "修改状态成功"
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //获取错误日志
        getErrorLog({ commit }, payload) {
            return axios.post('/api/task/errors', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_log', {
                            com_loglist: res.data.data.t,
                            com_totle: res.data.data.totalCount
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //获取kk日志列表
        getKKList({ commit }, payload) {
            return axios.post('/api/task/ims', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_kk_list', {
                            com_kklist: res.data.data.t,
                            com_totle: res.data.data.totalCount
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //获取email日志列表
        getEmailList({ commit }, payload) {
            return axios.post('/api/task/emails', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_email_list', {
                            com_emaillist: res.data.data.t,
                            com_totle: res.data.data.totalCount
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
    }
}